package com.lxyk.task;

import com.lxyk.domain.Classes;
import com.lxyk.domain.Job;
import com.lxyk.service.monthly.CourseInfoService;
import com.lxyk.service.monthly.JobService;
import com.lxyk.service.user.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//每天凌晨执行
@Component
public class TaskService {

    @Autowired
    private CourseInfoService courseInfoService;

    @Autowired
    private ClassesService classesService;

    @Autowired
    private JobService jobService;

    public void task() throws ParseException {
        System.out.println("定时任务执行");

        //1.扫描课程详情表,根据时间修改课程状态
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        Date parse = simpleDateFormat.parse(format);
        courseInfoService.updateUseFlag(parse);

        //2.扫描所有在读班级, 看是否有结课的班级,进行修改班级状态
        List<Classes> readClass = classesService.findReadClass();
        for (Classes aClass : readClass) {
            //查询每个班级存在的未上课程数量
            Integer infoCount = courseInfoService.findInfoCount(aClass.getId());
            if(infoCount == 0){ //数量=0,代表结课
                classesService.endClasses(aClass.getId());
                //班级结课，学生全部流入就业表
                jobService.addStudentJob(aClass.getId());
            }
        }

    }
}
